package com.inspur.incubator.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class randomImg {
	/**
	 * 生成图片验证码
	 * */
	@RequestMapping("admin/imgr")
    public void getAuthCode(HttpServletRequest request, HttpServletResponse response,HttpSession session)
            throws IOException {
		//根据图片大小修改
        int width = 85;
        int height = 24;
        Random random = new Random();
        //设置response头信息
        //禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        //生成缓冲区image类
        BufferedImage image = new BufferedImage(width, height, 1);
        //产生image类的Graphics用于绘制操作
        Graphics g = image.getGraphics();
        //Graphics类的样式
        g.setColor(Color.WHITE);
        Font font = new Font("黑体", Font.HANGING_BASELINE, 20);
        g.setFont(font);
        g.fillRect(0, 0, width, height);
        //绘制字符
        String strCode = "";
        StringBuffer s= new StringBuffer();
        String base = "ABCDEFGHGKLMNPQRSTUVWXYZ23456789";
        int size = base.length();
        for(int i=0;i<4;i++){
            String rand = String.valueOf(random.nextInt(10));
            strCode = strCode + rand;
            int start = random.nextInt(size);
            String strRand = base.substring(start, start + 1);
            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
            //根据图片大小、字符位置修改
            g.drawString(strRand+" ", i*21, 17);
            s.append(strRand);
          
        }
       
        //将字符保存到session中用于前端的验证
        //session.setAttribute("strCode", strCode);
        session.setAttribute("strCode", s.toString());
        g.dispose();

        ImageIO.write(image, "JPEG", response.getOutputStream());
        response.getOutputStream().flush();

    }
	
	/**
	 * 生成图片验证码
	 * */
	@RequestMapping("adminimgr")
    public void getAdminCode(HttpServletRequest request, HttpServletResponse response,HttpSession session)
            throws IOException {
        int width = 200;
        int height = 43;
        Random random = new Random();
        //设置response头信息
        //禁止缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        //生成缓冲区image类
        BufferedImage image = new BufferedImage(width, height, 1);
        //产生image类的Graphics用于绘制操作
        Graphics g = image.getGraphics();
        //Graphics类的样式
        g.setColor(Color.WHITE);
        Font font = new Font("黑体", Font.HANGING_BASELINE, 20);
        g.setFont(font);
        g.fillRect(0, 0, width, height);
        //绘制干扰线
        for(int i=0;i<50;i++){
            g.setColor(Color.gray);
        	// g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int x1 = random.nextInt(12);
            int y1 = random.nextInt(12);
            g.drawLine(100,100+x1,100+y1,100);
           // g.drawLine(x, y, x + x1, y + y1);
        }

        //绘制字符
        String strCode = "";
        StringBuffer s= new StringBuffer();
        String base = "ABCDEFGHIGKLMNPQRSTUVWXYZ23456789";
        int size = base.length();
        for(int i=0;i<4;i++){
            String rand = String.valueOf(random.nextInt(10));
            strCode = strCode + rand;
            int start = random.nextInt(size);
            String strRand = base.substring(start, start + 1);
            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
            g.drawString(strRand+"", i*50, 35);
            s.append(strRand);
          
        }
       
        //将字符保存到session中用于前端的验证
        //session.setAttribute("strCode", strCode);
        session.setAttribute("adminStrCode", s.toString());
        g.dispose();

        ImageIO.write(image, "JPEG", response.getOutputStream());
        response.getOutputStream().flush();

    }
	
	//创建颜色
    Color getRandColor(int fc,int bc){
        Random random = new Random();
        if(fc>255)
            fc = 255;
        if(bc>255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r,g,b);
    }

}
